// pc/03apply/beMasterWorker.js
var app = getApp()
var co = require('../../js/common.js')
var _this, auth_state = 0
//auth_state提交了名字和身份证号 值+100，上传了正面值+10，上传了正背面+1
Page({
   data: {
      pxArrShow: [0, 0],
      pxArr: [[0, 0], [0, 0]],
      idCard: {},
      userInfo: '',
      inputIsShow: 1
   },

   onLoad: function (opt) {
      _this = this
      //【1】获取用户认证信息
      this.getUserAuthInfo(opt)
   },
   //【1】获取用户认证信息
   getUserAuthInfo: function (param) {
      co.getShareApi('user/mg/getAuthInfo', param, function (res) {
         _this.setData({ userInfo: res.data })
         if (res.data.id_card_auth > 99) {
            _this.setData({ inputIsShow: 0 })
         }
         console.log('【_this.data】', _this.data)
         //【2】获取身份证正反面拍照图片
         _this.getIdCardImg(res.data.id_card_auth)
      })
   },
   //【2】获取身份证正反面拍照图片
   getIdCardImg: function (auth) {
      var user = app.aData.userInfo
      var imgUrl = 'https://img.ls293.cc/idcard/' + user.reg_from + '/' + user.unionid + '_idcard'
      console.log('【imgUrl】==' + imgUrl);
      var card = {}
      this.getImageInfo(imgUrl + 'back.jpg', function (res) {
         console.log('【idBack】res' + res)
         card.back = res
         _this.setData({ idCard: card })
      })

      this.getImageInfo(imgUrl + 'face.jpg', function (res) {
         console.log('【idFace】res' + res)
         card.face = res
         _this.setData({ idCard: card })
      })
   },
   //【3】获取图片信息
   getImageInfo: function (img, callback) {
      wx.getImageInfo({
         src: img,
         success: function (res) {
            console.log('【wx.getImageInfo success:res】', res)
            callback(img)
         },
         fail: function (res) {
            console.log('【wx.getImageInfo fail:res】', res)
         },
      })
   },

   changeInputShow: function () {
      this.setData({ inputIsShow: this.data.inputIsShow ? 0 : 1 })
      console.log('【this.data.inputIsShow】==', this.data.inputIsShow)
   },

   onReady: function () { },

   showLabel: function (e) {
      console.log('注册输入框e==', e)
      var l = e.detail.value.length
      if (l > 1) return;
      var index = e.currentTarget.id;
      var isShowArray = this.data.pxArrShow;
      isShowArray[index] = l ? 1 : 0;
      var pxArr = this.data.pxArr
      if (pxArr[index][0] == 0) {
         pxArr[index] = [20, e.target.offsetTop - 9]
         this.setData({ pxArr: pxArr })
      }
      this.setData({ pxArrShow: isShowArray })
   },

   authentication: function (e) {
      console.log('【提交认证信息】', e)
      var param = e.detail.value
      param.unionid = this.data.userInfo.unionid
      var auth = this.data.userInfo.id_card_auth
      if (auth < 100) {
         param.id_card_auth = auth + 100
      } else {
         param.id_card_auth = auth
      }
      co.getShareApi('user/mg/updateUserInfo', param, function (res) {
         _this.setData({
            userInfo: res.data,
            inputIsShow: 0
         })
      })
   },

   updateUserAuth: function (side) {
      var param = { unionid: this.data.userInfo.unionid }
      var auth = this.data.userInfo.id_card_auth
      if (side == 'face') {
         console.log('【auth % 100】==' + auth % 100)
         if (auth % 100 < 10) {
            param.id_card_auth = auth + 10
         } else {
            param.id_card_auth = auth
         }
      } else if (side == 'back') {
         console.log('【auth % 2】==' + auth % 2)
         if (auth % 2 == 0) {
            param.id_card_auth = auth + 1
         } else {
            param.id_card_auth = auth
         }
      }

      var reply = this.data.userInfo.auth_reply
      if (param.id_card_auth == auth && reply == '') {
         //如果状态id_card_auth没有改变，则不用更新，直接返回结束
         return
      } else if (auth == 111 && reply != '') {
         //如果是审核不通过有回复的情况
         param.auth_reply == ''
      }

      co.getShareApi('user/mg/updateUserInfo', param, function (res) {
         _this.setData({ userInfo: res.data })
         wx.setStorageSync('userInfo', res.data)
         app.aData.userInfo = res.data
      })
   },

   chooseImg: function (e) {
      if (this.data.userInfo.id_card_auth == 222) {
         co.modal('您已认证，不用再拍照了')
         return
      }
      console.log('【chooseImg:e】', e)
      wx.chooseImage({
         count: 1,
         sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
         sourceType: ['camera'], // 'album', 可以指定来源是相册还是相机，默认二者都有
         success: function (res) {
            console.log('【wx.chooseImage:res】', res)
            _this.uploadImg(res.tempFilePaths, e.currentTarget.id)
         },
         fail: function (res) { },
      })
   },

   uploadImg: function (filePath, id) {
      wx.uploadFile({
         url: co.shareApi + 'upload/file/img',
         filePath: filePath[0],
         name: 'image',
         formData: {
            unionid: app.aData.userInfo.unionid,
            side: id,
            reg_from: app.aData.userInfo.reg_from,
         },
         success: function (res) {
            _this.updateUserAuth(id)
            var obj = JSON.parse(res.data)
            _this.data.idCard[id] = obj.data
            _this.setData({ idCard: _this.data.idCard })
            var side = id == 'face' ? '正' : '背'
            co.toast('身份证' + '面上传成功')
         },
         fail: function (res) {
            console.log('uploadImg: fail: res', res);
         }
      })
   },

   onShareAppMessage: function () { }
})